<?php
class OrderDao {
	public  $table = 'order';
	private function searchWhere($where){
		if (!empty($where['start_time'])){
			$where['add_time'][] = array(strtotime($where['start_time']),'>=');
		}
		if (!empty($where['end_time'])){
			$where['add_time'][] = array(strtotime($where['end_time']),'<=');
		}
		
		$likeWhere = array('order_no');
		$where1 = array();
		foreach ($where as  $k=>$v){
			if (isset($v)&&$v!=""){
				if (in_array($k,$likeWhere) and !is_array($v)){
					$where1[$k] = array('%'.$v.'%' , 'like');
				}else{
					$where1[$k] =  $v;
				}
			}
		}

		return $where1;
	}
	
	public function getSumOrderAmount($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select sum(order_amount)-sum(discount_money)-sum(ticket_money) sumamount,count(1) as cnt from eo_order a  where  1=1";
		$sql.= $where;
		$sumOrder = ClassManager::getSql()->dataMutillist(' order and sumorder ',$sql);
		if(!empty($sumOrder)){
			return $sumOrder[0];
		}
		return 0;
	}
	
	public function getSumOrderAmountByfunduser($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select sum(user_money)+sum(online_money) sumamount ";
		$sql.= " from eo_order a left join eo_user b  on a.user_id=b.user_id ";
		$sql.= " where  1=1 ";
		$sql.= $where;
		$sumOrder = ClassManager::getSql()->dataMutillist(' order and sumorder by funduser ',$sql);
		if(!empty($sumOrder)){
			return $sumOrder[0][0];
		}
		return 0;
	}

	public function getSumOrderAmountGroupByYearmonth($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select sum(user_money)+sum(online_money) sumamount,substr(FROM_UNIXTIME(add_time),1,7) add_time ";
		$sql.= " from eo_order a ";  
		$sql.= " where  1=1";
		$sql.= $where;
		$sql.= " group by substr(FROM_UNIXTIME(a.add_time),1,7) ";
		
		$sumOrder = ClassManager::getSql()->dataMutillist(' order and sumorder by Year and Month ',$sql);
		return $sumOrder;
	}
	

	public function getSumOrderAmountGroupByFunduser($where){
		$where = $this->searchMutilWhere($where);
		$sql = "select sum(user_money)+sum(online_money) sumamount,substr(FROM_UNIXTIME(add_time),1,7) add_time ";
		$sql.= " from eo_order a left join eo_user b on a.user_id=b.id ";  
		$sql.= " where  1=1";
		$sql.= $where;
		$sql.= " group by b.fund_user_id,substr(FROM_UNIXTIME(a.add_time),1,7) ";
		
		$sumOrder = ClassManager::getSql()->dataMutillist(' order and sumorder by funduser ',$sql);
		return $sumOrder;
	}
	
	
	public function searchMutilWhere($where){
		$sql="";
	
		if(isset($where['order_status_list'])&&!empty($where['order_status_list'])){
			$sql.=" a.order_status in (".ORDER_STATUS_0.",".ORDER_STATUS_1.",".ORDER_STATUS_2.")";
		}
	
		if(isset($where['pay_status'])&&!empty($where['pay_status'])){
			$sql.=" and a.pay_status = ".$where['pay_status'];
		}
	
		if(isset($where['start_time'])&&!empty($where['start_time'])){
			$where['start_time']= strtotime($where['start_time']);
			$sql.=" and a.add_time >= '".$where['start_time']."'";
		}
	
		if(isset($where['end_time'])&&!empty($where['end_time'])){
			$where['end_time'] =  strtotime($where['end_time'])+ 86399;
			$sql.=" and a.add_time <= ".$where['end_time'];
		}
		
		if(isset($where['fund_user_id'])&&!empty($where['fund_user_id'])){
			$sql.=" and b.fund_user_id = ".$where['fund_user_id'];
		}
		
		if(isset($where['user_id'])&&!empty($where['user_id'])){
			$sql.=" and a.user_id = ".$where['user_id'];
		}
		
		return $sql;
	}
	
	public function searchList($where=array(),$order='',$limit=0){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->dataList1($this->table,'*',$where,$order,$limit);
	}
	public function searchData($where = array()){
		$where = $this->searchWhere($where);
		return ClassManager::getSql()->onedata1($this->table,'*',$where);
	}
	public function searchNumber($where){
		$where = $this->searchWhere($where);
		$tmp = ClassManager::getSql()->onedata1($this->table,"count(*)  ",$where);
		return $tmp[0];
	}
	public function add($data){
		return  ClassManager::getSql()->insert($this->table,$data);
	}
	public function update($data,$id){
		ClassManager::getSql()->updates($this->table,$data,"where id=".intval($id));
	}
	public function get($id){
		return ClassManager::getSql()->oneDataCache($this->table, 'id', intval($id));
	}
	public function getByOrderNo($orderNo){
		return ClassManager::getSql()->oneDataCache($this->table, 'order_no', $orderNo);
	}
	public function getByUserId($userId){
		return ClassManager::getSql()->oneDataCache($this->table, 'user_id', intval($userId));
	}
}

?>